package com.example.novelweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.ChainQuery;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.novelweb.common.BaseResponse;
import com.example.novelweb.common.ErrorCode;
import com.example.novelweb.common.ResultUtils;
import com.example.novelweb.constant.RedisConstant;
import com.example.novelweb.exception.BusinessException;
import com.example.novelweb.exception.ThrowUtils;
import com.example.novelweb.model.entity.Collect;
import com.example.novelweb.model.entity.Novel;
import com.example.novelweb.model.vo.NovelVO;
import com.example.novelweb.model.vo.UserVO;
import com.example.novelweb.service.CollectService;
import com.example.novelweb.mapper.CollectMapper;
import com.example.novelweb.service.NovelService;
import com.example.novelweb.service.RedisService;
import com.example.novelweb.service.TagService;
import com.example.novelweb.utils.UserUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author PERSON
* @description 针对表【collect(用户收藏)】的数据库操作Service实现
* @createDate 2025-01-26 22:32:41
*/
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
    implements CollectService{

  @Resource
  private RedisService redisService;

//  @Resource
//  private TagService tagService;

  //@Lazy 防止循环依赖
  //只在 add时save()前检测novelId有效性 时会用到
  @Lazy
  @Resource
  private NovelService novelService;

  @Override
  public BaseResponse<Boolean> add(Long novelId) {
    // 校验已登录
    UserVO userVO = UserUtils.LoginCheck();
    if( userVO== null) {
      throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR,"用户未登录");
    }

    Long userId = userVO.getId();
    // 判断存在相关记录
    Collect one = query().select("id","status").eq("userId", userId).eq("novelId", novelId).one();
    // one == null
    // one != null && one.getStatus == 1
    // one != null && one.getStatus() == 0  => 忽略
    if(one ==null) {
      // 校验novelId合法,存在
      Long count = novelService.query().eq("id",novelId).count();
      ThrowUtils.throwIf(count==0,ErrorCode.PARAMS_ERROR,"novelId无效");

      Collect collect = new Collect();
      collect.setUserId(userId);
      collect.setNovelId(novelId);
      boolean result =  save(collect);
      ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
    } else if(one.getStatus() == 1) {
      one.setStatus(0);
      boolean result = updateById(one);
      ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
    } else {
      return ResultUtils.success(true);
    }
    redisService.incrNovelCollectNum(novelId);

    return ResultUtils.success(true);
  }

  @Override
  public BaseResponse<Boolean> del(Long novelId) {
    // 校验已登录
    UserVO userVO = UserUtils.LoginCheck();
    if( userVO== null) {
      throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR,"用户未登录");
    }
    Long userId = userVO.getId();
    // 判断存在相关记录
    Collect one = query().select("id","status").eq("userId", userId).eq("novelId", novelId).one();
    // one == null                          => 忽略
    // one != null && one.getStatus() == 1  => 忽略
    // one != null && one.getStatus() == 0
    if(one != null && one.getStatus() == 0) {
      one.setStatus(1);
      boolean result = updateById(one);
      ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
      redisService.decrNovelCollectNum(novelId);
    }
    return ResultUtils.success(true);

  }

  @Override
  public BaseResponse<List<NovelVO>> getUserCollects(Long userId) {

    List<NovelVO> collectNovels = getBaseMapper().listNovelVOByUserId(userId);
//    for (NovelVO n:collectNovels) {
//      n.setTags(tagService.getTagsByNovelId(n.getId()));
//    }
    return ResultUtils.success(collectNovels);
  }


}




